Send a String[] ArrayList over Socket connection
Posted
by
Duncan Palmer
on Stack Overflow
See other posts from Stack Overflow
or by Duncan Palmer
Published on 2012-06-03T04:23:59Z
Indexed on
2012/06/03
4:41 UTC
Read the original article
Hit count: 143
So i'm trying to send a String[] Array/List over an open socket connection. I currently have this code:
Sending:
public void sendData() {
try {
OutputStream socketStream = socket.getOutputStream();
ObjectOutputStream objectOutput = new ObjectOutputStream(socketStream);
objectOutput.writeObject(new String[] {"Test", "Test2", "Test3"});
objectOutput.close();
socketStream.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
Recieving:
public Object readData() {
try {
InputStream socketStream = socket.getInputStream();
ObjectInputStream objectInput = new ObjectInputStream(new GZIPInputStream(socketStream));
Object a = objectInput.readObject();
return a;
} catch(Exception e) {
return null;
}
}
After I have recieved the String array/list on the other end I want to be able to iterate through it like I would do normally so I can get the values. My current code doesn't seem to works as it returns null as the value.
is this possible?
© Stack Overflow or respective owner